﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Bll;
using CSP.Common.Tools;
using CSP.Model.CSP;

namespace CSP.Web.UserControls
{
    public partial class HeadNavigation : CSPUserControlBase
    {
        private string _currentPagePath;

        private bool _isLoadCategory = true;

        /// <summary>
        /// 当前承载页面的路径
        /// </summary>
        public string CurrentPagePath
        {
            get
            {
                return _currentPagePath;
            }
        }

        /// <summary>
        /// 是否加载分类
        /// </summary>
        public bool IsLoadCategory
        {
            get { return _isLoadCategory; }
            set { _isLoadCategory = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentPagePath = this.Page.Request.Path;
        }

        /// <summary>
        /// 当程序未初始化控件时加载
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (this.Page.Request["getcategorydata"] != null)
            {
                List<ServiceType> serviceTypes = GetServiceTypes();
                string jsonServiceType = CommonTool.SerializeObject(GetCategoryDataByServiceTypes(serviceTypes));

                Response.Clear();
                Response.Write(jsonServiceType);
                Response.End();
            }
        }

        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        private List<CSP.Model.CSP.ServiceType> GetServiceTypes()
        {
            List<CSP.Model.CSP.ServiceType> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceTypeBll())
            {
                result = serviceTypeBll.FindAll().OrderBy(s => s.OrderId).ToList();
            }
            return result;
        }

        /// <summary>
        /// 根据服务类型获取分类数据
        /// </summary>
        /// <param name="serviceTypes"></param>
        /// <returns></returns>
        private List<CategoryData> GetCategoryDataByServiceTypes(List<ServiceType> serviceTypes)
        {
            List<CategoryData> result = new List<CategoryData>();
            for (int i = 0; i < serviceTypes.Count; i++)
            {
                result.Add(new CategoryData()
                {
                    Id = serviceTypes[i].Id,
                    ParentId = serviceTypes[i].ParentId,
                    Name = serviceTypes[i].Name,
                    Level = serviceTypes[i].Level,
                    OrderId = serviceTypes[i].OrderId,
                });
            }
            return result;
        }

        private class CategoryData
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public int OrderId { get; set; }

            public int ParentId { get; set; }

            public int Level { get; set; }
        }
    }
}